package com.jn.erp.wechat.staff.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.security.entity.LoginBody;
import com.jn.erp.security.entity.SysRole;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.security.entity.SysUserRole;
import com.jn.erp.security.sysservice.SysRoleService;
import com.jn.erp.security.sysservice.SysUserRoleService;
import com.jn.erp.security.sysservice.SysUserService;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderVo;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.institution.entity.vo.EmployeeVo;
import com.jn.erp.web.api.student.entity.dto.StudentRenewalDto;
import com.jn.erp.web.api.student.service.StudentService;
import com.jn.erp.web.api.sys.entity.SysOrgUser;
import com.jn.erp.web.api.sys.service.SysOrgUserService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <pre>
 *  教师端  我的相关接口管理
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 17:48
 */

@RestController
@RequestMapping("/wx/my")
public class MpMyController extends BaseWxAuthController {


    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysOrgUserService sysOrgUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TCourseOrderService courseOrderService;

    @Autowired
    private StudentService studentService;

    //获取用户信息
    @PostMapping("/getInfo")
    @Transactional
    public AjaxResult getInfo() {
        AjaxResult ajax = AjaxResult.success();
        WxUserDto wxUser=getWxUser();
        //获取用户信息
        SysUser sysUser = sysUserService.getById(wxUser.getUserId());
        EmployeeVo employee = new EmployeeVo();
        BeanUtil.copyProperties(sysUser, employee);
        QueryWrapper<SysOrgUser> qwou = new QueryWrapper<>();
        qwou.eq("org_id", wxUser.getOrgId());
        qwou.eq("user_id", sysUser.getId());
        SysOrgUser orgUser = sysOrgUserService.getOne(qwou);
        employee.setNickName(orgUser.getNickName());
        employee.setIsTeach(orgUser.getIsTeach());
        employee.setIsJob(orgUser.getIsJob());

        //获取角色列表
        QueryWrapper<SysUserRole> qw = new QueryWrapper<>();
        qw.eq("user_id", sysUser.getId());
//        qw.eq("org_id", wxUser.getOrgId());
        qw.in("org_id", "1", wxUser.getOrgId());
        List<SysUserRole> list = sysUserRoleService.list(qw);
        List<Long> roleIds = list.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        QueryWrapper<SysRole> qwr = new QueryWrapper<>();
        qwr.in("role_id", roleIds);
        qwr.in("org_id", "1", wxUser.getOrgId());
        qwr.eq("status", 0);
        List<SysRole> roles = sysRoleService.list(qwr);
        employee.setRoles(roles);

        ajax.put(AjaxResult.DATA_TAG, employee);
        return ajax;
    }

    //修改昵称
    @PostMapping("/updateNickName")
    @Transactional
    public AjaxResult updateNickName(@RequestBody EmployeeVo employeeVo) {
        WxUserDto wxUser=getWxUser();
        UpdateWrapper<SysOrgUser> up=new UpdateWrapper<>();
        up.set("nick_name",employeeVo.getNickName());
        up.eq("user_id",wxUser.getUserId());
        up.eq("org_id",wxUser.getOrgId());
        boolean update = sysOrgUserService.update(up);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }


    /**
     * 获取统计
     *
     * @return 用户统计信息
     */
    @GetMapping("/getCont")
    public AjaxResult getCont() {
        JSONObject ajax=new JSONObject();
        WxUserDto wxUser=getWxUser();
        //添加缴费预警数量/超时未点数量 （教师只能查看自己 校长可以看到全部）
            try {
                Map count = new HashMap();
                //超时未点数量
                TCourseOrderVo courseOrderVo = new TCourseOrderVo();
                courseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()));
                courseOrderVo.setOrgId(wxUser.getOrgIdLong());
                Long userIdIncompleteCount = courseOrderService.getUserIdIncompleteCount(courseOrderVo);
                count.put("incomplete",userIdIncompleteCount);
                //缴费预警数量
                StudentRenewalDto studentRenewalDto=new StudentRenewalDto();
                studentRenewalDto.setOrgId(wxUser.getOrgIdLong());
                studentRenewalDto.setCreateUser(String.valueOf(wxUser.getUserId()));
                Long userIdAlertsCount = studentService.getUserIdAlertsCount(studentRenewalDto);
                count.put("alerts",userIdAlertsCount);
                ajax.put("count", count);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
        return AjaxResult.success(ajax);
    }

}
